package game.users.dao.impl;

import java.sql.Connection;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import game.users.dao.IAUsersDao;
import game.users.pojo.AUsers;
import game.util.DBUtil;

public class AUsersDaoImpl implements IAUsersDao
{
	public int insertDao(AUsers users)
	{
		String sql = "INSERT INTO `game`.`a_users`(levelId,email,password,nickName,score,"
				+ "`name`, `content`, `status`, " + "`createTime`, `updateTime`, `pubTime`) VALUES "
				+ "(?,?,?,?,?,?,?,?,?,?,?)";
		DBUtil db = new DBUtil();
		Connection con = db.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try
		{
			ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
			/* 为占位符赋值 */
			ps.setObject(1, users.getLevelId());
			ps.setObject(2, users.getEmail());
			ps.setObject(3, users.getPassword());
			ps.setObject(4, users.getNickName());
			ps.setObject(5, users.getScore());
			ps.setObject(6, users.getName());
			ps.setObject(7, users.getContent());
			ps.setObject(8, users.getStatus());
			ps.setObject(9, users.getCreateTime());
			ps.setObject(10, users.getUpdateTime());
			ps.setObject(11, users.getPubTime());
			/* 影响的结果 */
			int res = ps.executeUpdate();
			/* 获取下一个主键 */
			rs = ps.getGeneratedKeys();
			if (rs.next())
			{
				/* 获取当前id */
				int id = rs.getInt(1);
				users.setId(id);
			}
			return res;
		} catch (SQLException e)
		{
			e.printStackTrace();
		} finally
		{
			db.closeAll(con, ps, rs);
		}
		return 0;
	}

	/* 更新一条信息 */
	public int updateDao(AUsers users)
	{
		/* 创建Sql语句 */
		String sql = "update a_users set levelId = ? ,email = ? , password = ? , " + "nickName = ? , score = ? , "
				+ "name = ? , content = ? , status = ? , " + "createTime = ? , updateTime = ? , pubTime = ? "
				+ "where id = ? ";
		/* 开始连接 */
		DBUtil db = new DBUtil();
		Connection con = db.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try
		{
			ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
			/* 为占位符赋值 */
			ps.setObject(1, users.getLevelId());
			ps.setObject(2, users.getEmail());
			ps.setObject(3, users.getPassword());
			ps.setObject(4, users.getNickName());
			ps.setObject(5, users.getScore());
			ps.setObject(6, users.getName());
			ps.setObject(7, users.getContent());
			ps.setObject(8, users.getStatus());
			ps.setObject(9, users.getCreateTime());
			ps.setObject(10, users.getUpdateTime());
			ps.setObject(11, users.getPubTime());
			ps.setObject(12, users.getId());
			/* 影响了几个结果 */
			int res = ps.executeUpdate();
			/* 获取下一个主键 */
			rs = ps.getGeneratedKeys();
			if (rs.next())
			{
				/* 设置id 从第一个开始的 */
				int id = rs.getInt(1);
				users.setId(id);
			}
			return res;
		} catch (SQLException e)
		{
			e.printStackTrace();
		} finally
		{
			db.closeAll(con, ps, rs);
		}
		return 0;
	}

	/* 删除一条记录 */
	public int deleteDao(AUsers users)
	{
		return 0;
	}

	/* 查找一条记录 */
	public AUsers findOndDao(Map<String, Object> condMap)
	{
		/* 为查询放置条件 */
		List<Object> paramsList = new ArrayList<Object>();
		/* 设置查询sql语句 */
		String sql = "select * from a_users ";
		if(condMap.get("email") != null 
			&& !"".equalsIgnoreCase(condMap.get("email") + ""))
		{
			/* 按照邮箱 */
			sql = sql + " where email = ? ";
			paramsList.add(condMap.get("email"));
		} else
		{
			/* 按照ID查询 */
			sql = sql + " where id = ?";
			paramsList.add(condMap.get("id"));
		}
		//System.out.println("=="+sql);
		/* 开始连接 */
		DBUtil db = new DBUtil();
		Connection con = db.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try
		{
			ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
			/* 赋值 */
			/* 为第几个占位符赋值 */
			int count = 1;
			for (Iterator iterator = paramsList.iterator(); iterator.hasNext();)
			{
				Object val = (Object) iterator.next();
				ps.setObject(count, val);
				count ++ ;
			}
			/* 获取下一个主键 */
			rs = ps.executeQuery();
			if (rs.next())
			{
				AUsers users = convertFromDb(rs);
				return users;
			}
		} catch (SQLException e)
		{
			e.printStackTrace();
		} finally
		{
			db.closeAll(con, ps, rs);
		}
		return null;
	}
	public List<AUsers> findCondListDao(Map<String, Object> conMap)
	{
		/* 储存结果 */
		List<AUsers> resultList = new ArrayList<AUsers>();
		/* 查询的sql语句 */
		String sql = "select * from a_users";
		/* 开始连接 */
		DBUtil db = new DBUtil();
		Connection con = db.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try
		{
			ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
			/* 获取下一个数据库中的主键 */
			rs = ps.executeQuery();
			while (rs.next())
			{
				AUsers users = convertFromDb(rs);
				/* 将结果放到Lsit中 */
				resultList.add(users);
			}
		} catch (SQLException e)
		{
			e.printStackTrace();
		} finally
		{
			db.closeAll(con, ps, rs);
		}
		return resultList;
	}

	private AUsers convertFromDb(ResultSet rs) throws SQLException
	{
		/* 把表中的记录和Java中的对象一一对应起来 */
		int id = rs.getInt("id");
		int levelId = rs.getInt("levelId");
		String email = rs.getString("email");
		String password = rs.getString("password");
		String nickName = rs.getString("nickName");
		int score = rs.getInt("score");
		String name = rs.getString("name");
		String content = rs.getString("content");
		byte status = rs.getByte("status");
		Date createTime = rs.getTimestamp("createTime");
		Date updateTime = rs.getTimestamp("updateTime");
		Date pubTime = rs.getTimestamp("pubTime");

		AUsers users = new AUsers();
		users.setLevelId(levelId);
		users.setEmail(email);
		users.setPassword(password);
		users.setNickName(nickName);
		users.setScore(score);
		users.setId(id);
		users.setName(name);
		users.setContent(content);
		users.setStatus(status);
		users.setCreateTime(createTime);
		users.setUpdateTime(updateTime);
		users.setPubTime(pubTime);
		return users;
	}

}
